package com.ciei.dpagm.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/** Druid 配置
 * @author kiho
 * @date 2021/05/21 11:19
 **/
@Configuration
public class DruidConfig {

    /**
     * Druid 监控
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet() {
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        Map<String, String> initParams = new HashMap<>();
        //登录账号和密码
        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","ciei");
        bean.setInitParameters(initParams);
        return bean;
    }

    @Bean
    public FilterRegistrationBean<WebStatFilter> webStatFilter() {
        FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<>();
        //配置不进行统计的例外
        initParams.put("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/static/*,/druid/*");
        bean.setInitParameters(initParams);
        //配置统计过滤器适用模式
        bean.setUrlPatterns(Collections.singletonList("/*"));
        return bean;
    }
}
